package com.sdtv.mediunfusion.mvp.ui.outsidetask;

import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sdtv.basemodule.annotations.CreatePresenter;
import com.sdtv.basemodule.annotations.PresenterVariable;
import com.sdtv.basemodule.base.BaseActivity;
import com.sdtv.basemodule.router.ARouterUtils;
import com.sdtv.mediunfusion.R;
import com.sdtv.mediunfusion.bean.SelectTopicBean;
import com.sdtv.mediunfusion.mvp.contract.MainContract;
import com.sdtv.mediunfusion.mvp.presenter.SelectTopicListPresenter;
import com.sdtv.mediunfusion.mvp.ui.outsidetask.adapter.SelectTopicAdapter;
import com.sdtv.mediunfusion.utils.Constants;
import com.sdtv.mediunfusion.utils.RouterPath;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

@Route(path = RouterPath.TO_SELECT_TOPIC_LIST_ACTIVITY)
@CreatePresenter(presenter = SelectTopicListPresenter.class)
public class MySelectTopicActivity extends BaseActivity implements MainContract.SelectTopicListView {


    @BindView(R.id.taskListView)
    RecyclerView listView;
    @BindView(R.id.taskList_xRefreshView)
    SmartRefreshLayout refreshLayout;
    //列表显示内容adapter
    private SelectTopicAdapter adapter;
    // 列表数据
    private List<SelectTopicBean> dataList = new ArrayList<>();
    //当前页
    private int currentPage = 0;
    @PresenterVariable
    SelectTopicListPresenter presenter;

    /**
     * 布局文件
     *
     * @return
     */
    @Override
    public int getLayoutId() {
        return R.layout.activity_my_select_topic;
    }

    /**
     * 初始化页面View
     */
    @Override
    public void initView() {
        adapter = new SelectTopicAdapter(R.layout.select_topic_items_layout, dataList);
        listView.setLayoutManager(new LinearLayoutManager(this));
        listView.setAdapter(adapter);
        //设置刷新事件
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            // 刷新列表数据
            dataList = new ArrayList<>();
            currentPage = 0;
            initData();
        });
        // 设置加载更多事件
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            currentPage += Constants.PAGE_SIZE;
            initData();
        });
        //设置listview的点击事件--点击跳转到查看详情页面
        adapter.setOnItemClickListener((adapter, view, position) -> {
            SelectTopicBean item = (SelectTopicBean) adapter.getItem(position);
            HashMap<String, String> map = new HashMap<>();
            map.put("selectTopicId", item.getSelectTopicId());
//            RouteSkip.pathCompose(MySelectTopicActivity.this,
//                    RouterConfig.ADD_AND_SHOW_SELECT_TOPIC, map, true);
        });
        // 子View的点击事件
        adapter.setOnItemChildClickListener((adapter, view, position) -> {
            //点击查看审核状态---
            SelectTopicBean item = (SelectTopicBean) adapter.getItem(position);
            Bundle bundle = new Bundle();
            bundle.putString("selectTopicTitle", item.getSelectTopicTitle());
            bundle.putString("selectTopicId", item.getSelectTopicId());
            ARouterUtils.navigation(RouterPath.TO_TOPICLOGGING_ACTIVITY, bundle);

        });
    }

    /**
     * 加载页面数据
     */
    @Override
    public void initData() {
        presenter.getSelectListData(currentPage);
    }

    @Override
    public void setListData(int totalCount, List<SelectTopicBean> responseList) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        dataList.addAll(responseList);
        adapter.setNewData(dataList);
        // 判断是否显示加载更多
        if (totalCount <= dataList.size()) {
            //全部数据已经加载完成 不显示加载更多
            refreshLayout.finishLoadMoreWithNoMoreData();
        }
    }

    /**
     * 未加载到数据
     * 显示暂无或者加载完成
     */
    @Override
    public void showEmptyView() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        if (null != adapter && adapter.getData().isEmpty()) {
            // 显示暂无内容
            adapter.setEmptyView(R.layout.select_list_nocontent_layout, listView);
        } else {
            refreshLayout.finishLoadMoreWithNoMoreData();
        }
    }

    /**
     * 加载数据失败了
     */
    @Override
    public void onError() {
        //显示是被View 或者是刷新 加载失败tip--在base里面统一处理
        super.onError();
    }
}
